home *** CD-ROM | disk | FTP | other *** search
- Path: news.voicenet.com!news
- From: kobak@voicenet.com (Peter Kobak)
- Newsgroups: comp.lang.c++
- Subject: Re: Will it be auto-deleted?
- Date: 20 Feb 1996 14:31:58 GMT
- Organization: Voicenet - Internet Access - (215)674-9290
- Message-ID: <4gcm0u$6ou@news.voicenet.com>
- NNTP-Posting-Host: ivyland236.voicenet.com
- X-Newsreader: NeoLogic News for OS/2 [version: 4.2]
-
- In message <1996Feb20.110314.46035@yogi.urz.unibas.ch> -
- song@iso.iso.unibas.ch (Song Jin) writes:
- :>
- :>void myfunction(void)
- :>{
- :> double *mypointer = new double[100];
- :>}
- :>
- :>The mypointer and the buffer it pointed will be auto-deleted after returned
- :>from myfunction, am I right?
-
- You are wrong. Allocations from 'new' must always be 'delete'd. You could:
- 1) manually delete the pointer when you're done with it (not a good choice if
- you've got exceptions), 2) use a "smart" pointer object that knows to delete
- itself (see the FAQ or a decent book for a description of smart pointers), or
- 3) just declare your array directly as an auto variable. If you're not in an
- OS that has problems with stack size, IMHO choice 3 is the best, fastest, and
- simplest.
-
- ================
- Peter Kobak
- kobak@voicenet.com
-
-
-